package com.kuwei.servlet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kuwei.base.ParamerException;
import com.kuwei.base.ServiceException;
import com.kuwei.model.User;
import com.kuwei.mybatis.pages.PageMap;
import com.kuwei.service.UserService;
import com.kuwei.servlet.frame.BaseServlet;
import com.kuwei.utils.CommonUtils;
import com.kuwei.utils.Constants;
import com.kuwei.utils.MD5Encrypt;
import com.kuwei.utils.ReturnResult;
import com.kuwei.utils.ServiceConstants;
import com.kuwei.utils.SessionUtils;
import com.kuwei.utils.Utility;
/**
 * 用户Servlet
 * @author zyc
 *
 */
@Service("userServlet")
public class UserServlet extends BaseServlet{
	public UserServlet() {
		
	}
	@Autowired
	UserService userService;
	
	public void getCurrLoginUser(HttpServletRequest request,HttpServletResponse response,Map<String, Object> rtnMap) throws ServiceException, ParamerException{	
		ReturnResult result = new ReturnResult();
		User user = SessionUtils.getUserInfo(request);
		user.setPassWd("");
		result.setContent(Constants.SUCCESS, null, user);
		rtnMap.put("data", result.getContent());
	}
	
	public void getUserList(HttpServletRequest request,HttpServletResponse response,Map<String, Object> rtnMap) throws ServiceException, ParamerException{	
		ReturnResult result = new ReturnResult();
		Map<String, Object> param = Utility.Params.parseParams(request);
		Map<String,Object> reqMap = new HashMap<String, Object>();
		User user = SessionUtils.getUserInfo(request);
		if(!CommonUtils.isEmpty(user.getUser_id())){
			if(!Constants.ADMIN_CODE.equals(user.getType())){
				reqMap.put("USER_ID", user.getUser_id());
			}
			reqMap.put("STATUS_CD", param.get("STATUS_CD"));
			reqMap.put("USER_NAME", param.get("USER_NAME"));
			reqMap.put("REAL_NAME", param.get("REAL_NAME"));
			reqMap.put("TYPE", param.get("TYPE"));
			PageMap pm = new PageMap();
			pm.putAll(reqMap);
			String pageSize = request.getParameter("pageSize");
			if(CommonUtils.isEmpty(pageSize)){
				pageSize = "10";
			}
			
			String startNum = request.getParameter("startNum");
			if(CommonUtils.isEmpty(startNum)){
				startNum = "0";
			}
			pm.setPageSize(Integer.valueOf(pageSize));
			pm.setStartNum(Integer.valueOf(startNum));
			List<Map<String, Object>> list = null;
			try {
				list= sqlSessionDaoSupport.getSqlSession().selectList(ServiceConstants.QUERY_USER_LIST, pm);
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("dataList", list);
				map.put("totleRows",pm.getTotalRecord());
				result.setContent(Constants.SUCCESS, null,map);
			} catch (Exception e){
				result.setContent(Constants.FAIL, e.getMessage());
			} finally{
				rtnMap.put("data", result.getContent());
			}
		} else{
			throw new ParamerException("Parameter[user] is empty, please check it!");
		}
	}
	public void updateUserInfo(HttpServletRequest request,HttpServletResponse response,Map<String, Object> rtnMap) throws ServiceException, ParamerException{	
		ReturnResult result = new ReturnResult();
		Map<String, Object> param = Utility.Params.parseParams(request);
		User user = SessionUtils.getUserInfo(request);
		if(!CommonUtils.isEmpty(user.getUser_id())&&!CommonUtils.isEmpty(param.get("USER_ID"))){
			param.put("UPDATE_USER", user.getUser_id());
			if(!CommonUtils.isEmpty(param.get("PASSWD"))){
				String passwd=MD5Encrypt.md5Encode(param.get("PASSWD").toString());
				param.put("PASSWD", passwd);
			}
			Map<String, Object> resultMap = null;
			try {
				resultMap= userService.updateUser(param);
				result.setContent(Constants.SUCCESS, null,resultMap);	
			} catch (Exception e){
				result.setContent(Constants.FAIL, null,resultMap);	
				result.setContent(Constants.FAIL, e.getMessage());
			} finally{
				rtnMap.put("data", result.getContent());
			}
		} else{
			throw new ParamerException("Parameter[user_id] is empty, please check it!");
		}
	}
	
	public void getUserInfo(HttpServletRequest request,HttpServletResponse response,Map<String, Object> rtnMap) throws ServiceException, ParamerException{	
		ReturnResult result = new ReturnResult();
		Map<String, Object> param = Utility.Params.parseParams(request);
		Map<String,Object> reqMap = new HashMap<String, Object>();
		User user = SessionUtils.getUserInfo(request);
		if(!CommonUtils.isEmpty(user.getUser_id())&&!CommonUtils.isEmpty(param.get("USER_ID"))){
			reqMap.put("USER_ID", param.get("USER_ID"));
			List<Map<String, Object>> list = null;
			try {
				list= sqlSessionDaoSupport.getSqlSession().selectList(ServiceConstants.QUERY_USER_LIST, reqMap);
				result.setContent(Constants.SUCCESS, null,list);
			} catch (Exception e){
				result.setContent(Constants.FAIL, e.getMessage());
			} finally{
				rtnMap.put("data", result.getContent());
			}
		} else{
			throw new ParamerException("Parameter[user] is empty, please check it!");
		}
	}
	
	public void addUserInfo(HttpServletRequest request,HttpServletResponse response,Map<String, Object> rtnMap) throws ServiceException, ParamerException{	
		ReturnResult result = new ReturnResult();
		Map<String, Object> param = Utility.Params.parseParams(request);
		Map<String,Object> reqMap = new HashMap<String, Object>();
		User user = SessionUtils.getUserInfo(request);
		if(!CommonUtils.isEmpty(user.getUser_id())){
			param.put("CREATE_USER", user.getUser_id());
			param.put("STATUS_CD", Constants.STATE_1000);
			String mobile_no = param.get("MOBILE_NO").toString();
			//密码为手机号码后6位
			String passwd = MD5Encrypt.md5Encode(mobile_no.substring(mobile_no.length()-6));
			param.put("PASSWD", passwd);
			if(!CommonUtils.isEmpty(param.get("MEAL_SUB"))){
				param.put("MEAL_SUB", Float.valueOf(param.get("MEAL_SUB").toString())*100);
			}
			if(!CommonUtils.isEmpty(param.get("OVER_TIME_SUB"))){
				param.put("OVER_TIME_SUB", Float.valueOf(param.get("OVER_TIME_SUB").toString())*100);
			}
			if(!CommonUtils.isEmpty(param.get("WAGE"))){
				param.put("WAGE", Float.valueOf(param.get("WAGE").toString())*100);
			}
			Map<String, Object> resultMap = null;
			try {
				resultMap= userService.insertUser(param);
				result.setContent(Constants.SUCCESS, null,resultMap);	
			} catch (Exception e){
				result.setContent(Constants.FAIL, null,resultMap);	
				result.setContent(Constants.FAIL, e.getMessage());
			} finally{
				rtnMap.put("data", result.getContent());
			}
		} else{
			throw new ParamerException("Parameter[user_id] is empty, please check it!");
		}
	}
}
